package com.epro.dx.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public class CommonLVAdapter<T> extends BaseAdapter {
    protected List<T> mDatas;

    protected final int mItemLayoutId;

    protected int countSum = -1;

    private int variableId;
    private OnLongItemClickListener mLongListener;
    private OnItemClickListener mItemCLickListener;

    public CommonLVAdapter(List<T> mDatas, int variableId, int itemLayoutId) {
        this.mDatas = mDatas;
        this.mItemLayoutId = itemLayoutId;
        this.variableId = variableId;
    }

    /**
     * 替换元素并刷新
     *
     * @param mDatas
     */
    public void refresh(List<T> mDatas) {
        this.mDatas = mDatas;
        this.notifyDataSetChanged();
    }

    /**
     * 删除元素并更新
     *
     * @param position
     */
    public void deleteList(int position) {
        this.mDatas.remove(position);
        this.notifyDataSetChanged();
    }

    /**
     * 定义itemCount
     */
    public CommonLVAdapter setCount(int i) {
        countSum = i;
        this.notifyDataSetChanged();
        return this;
    }

    @Override
    public int getCount() {
        if (countSum == -1) {
            return mDatas.size();
        } else {
            return countSum;
        }
    }

    @Override
    public T getItem(int position) {
        if (countSum == -1) {
            return mDatas.get(position);
        } else {
            return mDatas.get(countSum % mDatas.size());
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
//        final ViewHolder viewHolder = getViewHolder(position, convertView,
//                parent);
//        fillData(viewHolder, position, getItem(position));
//
//        return viewHolder.getConvertView();

        // 没有ViewHolder的复用，但Databinding内部已经实现了复用
        ViewDataBinding binding;
        if (convertView == null) {

            binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), mItemLayoutId, parent, false);
        } else {
            binding = DataBindingUtil.getBinding(convertView);
        }
        binding.setVariable(variableId, mDatas.get(position));
        reFillData(binding, position, getItem(position));
        //设置长按删除事件
        binding.getRoot().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mLongListener != null) {
                    mLongListener.onLongItemClick((T) mDatas.get(position), position);
                }
                return false;
            }
        });
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemCLickListener != null) {
                    mItemCLickListener.onItemClick(mDatas.get(position), position);
                }
            }
        });
        return binding.getRoot();
    }

    protected void reFillData(ViewDataBinding binding, int position, T item) {

    }

    public interface OnLongItemClickListener<T> {
        void onLongItemClick(T model, int position);
    }

    public void setOnLongItemClickListener(OnLongItemClickListener longItemClickListener) {
        this.mLongListener = longItemClickListener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(T model, int position);
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.mItemCLickListener = itemClickListener;
    }
}
